<template>
	<view class="welook">
		<swiper :indicator-dots="true" :circular="true" :duration="1000">
			<swiper-item v-for="(item, index) in looklist" :key="index">
				<view class="swiper-item">
					<view class="lookArea">
						<view class="tu">
							<image :src="item.img" mode=""></image>
						</view>
						<view class="info">
							<text class="title">{{item.name}}</text>
							<text class="conte">{{item.desc}}</text>
						</view>
						<view class="btnGroup">
							<button v-if="item.ifsuccess" disabled="">已加入</button>
							<button v-else="item.ifsuccess" :data-item="JSON.stringify(item)" :data-index="index" @click="addShujia">加入书架</button>
							<button :data-id="item.book_id" @click="toread">读读看看</button>
						</view>
					</view>
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import key from '@/common/key.js';
	import utils from '@/common/utils.js';
	export default {
		data() {
			return {
				looklist: ''
			}
		},
		onLoad(option) {
			let modeEncode = option.rankDatailData;
			let modeDecode = decodeURIComponent(modeEncode)
			let mode = JSON.parse(utils.decode(modeDecode))
			this.looklist = mode;
			console.log(this.looklist);
		},
		created() {
			var shujiaList = uni.getStorageSync('addshujiaData');
			this.looklist.forEach((val2, index2, arr2) => {
				this.looklist[index2].ifsuccess = false;
			})
			if(shujiaList.length>0){
				shujiaList.forEach((val, index, arr) => {
					this.looklist.forEach((val2, index2, arr2) => {
						if(val.book_id == val2.book_id){
							this.looklist[index2].ifsuccess = true;
						}
					})
				})
			}
			console.log(shujiaList, this.looklist)
		},
		methods: {
			toread(e){
				var id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: '/pages/childPages/bookDetail/bookDetail?book_id=' + id
				})
			},
			addShujia(e){
				this.$forceUpdate();
				var item = JSON.parse(e.currentTarget.dataset.item);
				var index = e.currentTarget.dataset.index;
				this.looklist[index].ifsuccess = true;
				var ifsuccess = utils.addshujia(item);
				
				// var id = e.currentTarget.dataset.id;
				// const data = {
				// 	"app": "YLXS_Android",
				// 	"channel": "H5",
				// 	"device_id": 0,
				// 	"t_channel": "YLXS_Android",
				// 	"version": "1.0.0",
				// 	"book_ids": id,
				// 	"user_id": '0',
				// 	"chapter_ids": '0'
				// }
				// 			
				// let sign = utils.sortObj(data)
				// uni.request({
				// 	url: `${key.URL}a3/bookrack/add`,
				// 	method: 'POST',
				// 	data: {
				// 		"app": "YLXS_Android",
				// 		"channel": "H5",
				// 		"device_id": 0,
				// 		"t_channel": "YLXS_Android",
				// 		"version": "1.0.0",
				// 		"book_ids": id,
				// 		"user_id": '0',
				// 		"chapter_ids": '0',
				// 		"sign": sign,
				// 	},
				// 	dataType: 'json',
				// 	header: {
				// 		"Content-Type": "application/x-www-form-urlencoded"
				// 	},
				// 	success: (res) => {
				// 		if (res.data.status == 200) {
				// 			console.log(res);
				// 			// var bidArr = book_id.split(',');
				// 			// for(var i = 0; i<bidArr.length; i++){
				// 			// 	this.sjList.forEach((val, index, arr) => {
				// 			// 		if(val.bid == bidArr[i]){
				// 			// 			this.sjList.splice(index, 1); 
				// 			// 		}
				// 			// 	})
				// 			// }
				// 			uni.showToast({
				// 				title: "添加成功"
				// 			})
				// 		}
				// 	}
				// })
			}
		}
	}
</script>

<style scoped>
	uni-page-body{
		height: 100%;
	}
	.welook{
		width: 100%;
		height: 100%;
	}
	uni-swiper{
		height: 100%;
	}
	uni-swiper >>> .uni-swiper-dot{
		height: 4px;
		border-radius: 2px;
		background: #DFDFDF;
	}
	uni-swiper >>> .uni-swiper-dot-active {
		background-color: #4F50CE;
		width: 18px;
		height: 4px;
		border-radius: 2px;
	}
	.swiper-item{
		width: 100%;
	}
	.lookArea{
		width: 100%;
	}
	.tu{
		width: 428upx;
		height: 560upx;
		margin: 0 auto;
		margin-top: 90upx;
		background: url(../../../../static/mask.png);
		background-size: 100% 100%;
	}
	.tu image{
		display: block;
		float: left;
		width: 396upx;
		height: 528upx;
		margin-left: 4upx;
		margin-top: 4upx;
	}
	.info{
		width: 490upx;
		margin: 0 auto;
	}
	.info .title{
		display: block;
		width: 100%;
		text-align: center;
		color: #4B4C5D;
		font-size: 36upx;
		line-height: 50upx;
		margin-top: 30upx;
	}
	.info .conte{
		font-size: 28upx;
		color: #818295;
		display: block;
		width: 100%;
		text-align: center;
		line-height: 46upx;
		margin-top: 10upx;
		max-height: 300upx;
		overflow: auto;
	}
	.btnGroup{
		width: 490upx;
		margin: 0 auto;
		margin-top: 70upx;
	}
	.btnGroup button{
		width: 230upx;
		height: 80upx;
		float: left;
		margin-left: 30upx;
		border: none;
		font-size: 28upx;
		line-height: 80upx;
		color: #fff;
		background: #4F50CE;
	}
	.btnGroup button:first-child{
		margin-left: 0;
		background: #EE9067;
	}
</style>
